/** 
 ** Copyright (c) 2010 Ushahidi Inc
 ** All rights reserved
 ** Contact: team@ushahidi.com
 ** Website: http://www.ushahidi.com
 ** 
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser
 ** General Public License version 3 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file. Please review the following information to
 ** ensure the GNU Lesser General Public License version 3 requirements
 ** will be met: http://www.gnu.org/licenses/lgpl.html.	
 **	
 **
 ** If you have questions regarding the use of this file, please contact
 ** Ushahidi developers at team@ushahidi.com.
 ** 
 **/

package com.ushahidi.android.app;

import java.util.ArrayList;
import java.util.List;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

import com.ushahidi.android.app.checkin.CheckinMap;
import com.ushahidi.android.app.checkin.ListCheckin;
import com.ushahidi.android.app.data.IncidentsData;

public class AroundMe extends TabActivity
{
    private TabHost          tabHost;

    private Bundle           bundle;
    
    public static List<IncidentsData> ResourcesInMapView = new ArrayList<IncidentsData> ();

    private int              catid;

    //private Bundle           extras;

    private TextView         activityTitle;

    private static final int VIEW_SEARCH = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.around_me_tab);
	new Handler();
	bundle = new Bundle();
	//extras = this.getIntent().getExtras();
	activityTitle = (TextView) findViewById(R.id.title_text);
	tabHost = getTabHost();

	// load preferences
	checkinEnabled();

	tabHost.setCurrentTab(1);//Display Map View at first

	// set tab colors
	setTabColor(tabHost);

	// set tab colors on tab change as well
	tabHost.setOnTabChangedListener(new OnTabChangeListener()
	{
	    public void onTabChanged(String arg0)
	    {	
	    	setTabColor(tabHost);
	    }
	});

//	if (extras != null)
//	{
//	    catid = extras.getInt("catid");// .getBundle("selected_catid");
//	    tabHost.setCurrentTab(1);
//	}
    }

    @Override
    public void onResume()
    {
	super.onResume();
    }

    public void checkinEnabled()
    {

	UshahidiPref.loadSettings(AroundMe.this);
	if (UshahidiPref.isCheckinEnabled == 1)
	{
	    setTitle(getString(R.string.tab_item_checkin));
	    if (activityTitle != null)
		activityTitle.setText(getString(R.string.tab_item_checkin));
	    tabHost.addTab(tabHost
		    .newTabSpec("list_checkins")
		    .setIndicator(
		            getString(R.string.tab_item_report_list),
		            getResources().getDrawable(
		                    R.drawable.ushahidi_tab_reports_selected))
		    .setContent(new Intent(AroundMe.this, ListCheckin.class)));

	    // checkins
	    tabHost.addTab(tabHost
		    .newTabSpec("checkin")
		    .setIndicator(
		            getString(R.string.tab_item_report_map),
		            getResources().getDrawable(
		                    R.drawable.ushahidi_tab_checkin_selected))
		    .setContent(new Intent(AroundMe.this, CheckinMap.class)));
	}
	else
	{
	    //Set title
	    if (activityTitle != null)
		activityTitle.setText(getTitle());
	    //List View
	    tabHost.addTab
	    (
		tabHost.newTabSpec("list_reports")
			.setIndicator(	
				getString(R.string.tab_item_report_list),
				getResources().getDrawable(R.drawable.ushahidi_tab_reports_selected))
			.setContent(new Intent(AroundMe.this, AroundMeList.class))
	    );// TODO To be changed
	    //Map View
	    tabHost.addTab
	    (
		    tabHost.newTabSpec("map")
		    	.setIndicator(
		    		getString(R.string.tab_item_report_map),
		    		getResources().getDrawable(R.drawable.ushahidi_tab_map_selected))
		    	.setContent(new Intent(AroundMe.this, AroundMeMap.class)
		        .putExtra("MainMap", true)));


	    //TODO OceanMaster, To be changed
	}

    }

    public static void setTabColor(TabHost tabhost)
    {
	for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++)
	{
	    // unselected
	    tabhost.getTabWidget().getChildAt(i)
		    .setBackgroundColor(Color.parseColor("#990000"));// #404041

	    TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i)
		    .findViewById(android.R.id.title); // Unselected Tabs
	    tv.setTextColor(Color.parseColor("#ffffff"));
	}
	// selected
	tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
	        .setBackgroundColor(Color.parseColor("#FFCC00"));// #8A1F03
	TextView tv = (TextView) tabhost.getCurrentTabView().findViewById(
	        android.R.id.title);
	tv.setTextColor(Color.parseColor("#ffffff"));
    }

    public void onClickHome(View v)
    {
	goHome(this);
    }

    protected void goHome(Context context)
    {
	final Intent intent = new Intent(context, Ushahidi.class);
	intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	context.startActivity(intent);
    }

    public void onSearchDeployments(View v)
    {
	Intent intent = new Intent(AroundMe.this, DeploymentSearch.class);
	startActivityForResult(intent, VIEW_SEARCH);
	setResult(RESULT_OK);
    }

}
